/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.plant.serviceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.plant.dao.BaseDao;
import com.plant.entity.PgcArticle;
import com.plant.entity.PgcTopic;
import com.plant.entity.PgcVideoArticle;
import com.plant.entity.UgcCollect;
import com.plant.entity.UgcComment;
import com.plant.entity.UgcUser;
import com.plant.service.FindService;

@Service                                                                        
@Transactional
public class FindServiceImpl implements FindService{
	@Resource(name = "baseDaoImpl")                                          
	private BaseDao baseDao;


	@SuppressWarnings("unchecked")
	public List<Object[]> listall(Integer page,String time) {//--1.查看显示官方话题
		String hql="select a.id,a.user_id,a.title,a.image_url,a.heat,a.pv,a.create_time from pgc_topic as a where create_time<? order by create_time desc";
		return baseDao.findfenye((page-1)*10, 10, hql, time,PgcTopic.class);
	}

	public Long topiccount(PgcTopic pgt,Object obj,String time) {//--统计话题的评论个数
		String hql="select count(id) from UgcTopicJoin where toTopicId=("+obj+") and createTime>'("+time+")'";
		Long lo=baseDao.CountHql(hql);
		return lo;
	}
	public Long alltopiccount(PgcTopic pgt,Object obj) {//--统计话题的评论个数
		String hql="select count(id) from UgcTopicJoin where toTopicId=("+obj+")";
		Long lo=baseDao.CountHql(hql);
		return lo;
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> listArticle(Integer page,String time) {//--2.查看官方文章推荐方法                     
		String hql="select a.id,a.user_id,a.title,a.image,a.pv,b.nickname,b.photo,a.add_time from pgc_article as a,ugc_user as b where a.user_id=b.id and add_time<? order by add_time desc";//--通过 时间分页查询 显示10条
		return baseDao.findfenye((page-1)*10, 10, hql, time,null);
	}
	
	@SuppressWarnings("unchecked")
	public List<Map<String,Object>> listActivity(Integer page,String time) {//查看官方活动
		String sql="SELECT a.user_id,a.title,a.image,a.maxnum,a.city,a.province,a.start_date,a.end_date,b.nickname,b.photo FROM pgc_activity a,ugc_user b " +
				"WHERE a.user_id=b.id AND create_time<'"+time+"' ORDER BY create_time DESC LIMIT "+(page-1)*10+",10";
		return baseDao.Bysql(sql, null);
	}


	@SuppressWarnings("unchecked")
	public List<Object[]> listoneArticle(PgcArticle pa) {//--3.查看显示一片文章详情
		List<Object[]> list=new ArrayList<Object[]>();
		String hql="select content,image,pv from PgcArticle where id=("+pa.getId()+")";
		list=baseDao.find(hql);
		return list;
	}
	@SuppressWarnings("unchecked")
	public List<Object[]> listoneTopic(PgcTopic pt) {//--3.查看显示话题详情
		List<Object[]> list=new ArrayList<Object[]>();
		String hql="select A.id,A.title,A.pv,A.imageUrl,A.content,A.userId,B.nickname,B.photo from PgcTopic as A,UgcUser as B where A.userId=B.id and A.id=("+pt.getId()+")";
		list=baseDao.find(hql);
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> listTopicComment(PgcTopic pt,Integer page,String time) {//--查看显示话题参与最新评论
		List<Object[]> list=new ArrayList<Object[]>();
		String hql="select A.id,A.user_id,A.create_time,A.to_topic_id,A.content,B.nickname,B.photo from ugc_topic_join as A,ugc_user as B where A.user_id=B.id and A.to_topic_id=("+pt.getId()+") and create_time<? order by create_time desc";
		list=baseDao.findfenye((page-1)*10, 10, hql, time,PgcTopic.class);
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> listHotTopicComment(PgcTopic pt,String time) {//--查看最热评论
		List<Object[]> list=new ArrayList<Object[]>();
		String hql="SELECT U.id AS userid, U.nickname, U.photo, J.id, J.user_id, J.content, J.to_topic_id, J.create_time, count( L.to_content_id ) AS like_count FROM `ugc_topic_join` AS J " +
				"INNER JOIN `ugc_user` AS U ON J.`user_id`=U.`id` INNER JOIN `ugc_like` AS L ON L.to_content_id = J.id WHERE J.to_topic_id=("+pt.getId()+") AND L.to_content_type=4 AND J.create_time<'"+time+"' " +
				"GROUP BY L.to_content_id ORDER BY like_count DESC , L.id ASC LIMIT 3";
		list=baseDao.Bysql(hql, PgcTopic.class);
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> listAllTopicComment(PgcTopic pt) {//--查看所有话题参与讨论个数
		List<Object[]> list=new ArrayList<Object[]>();
		String hql="select A.id,A.user_id,A.create_time,A.to_topic_id,A.content,B.nickname,B.photo from ugc_topic_join as A,ugc_user as B where A.user_id=B.id and A.to_topic_id=("+pt.getId()+")";
		list=baseDao.Bysql(hql, PgcTopic.class);
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> listTopicCommentImage(Object obj) {//----查看参与讨论 发布的图片
		List<Object[]> list=new ArrayList<Object[]>();
		String hql="select url from UgcUploadImages where fromContentType=4 and fromContentId="+obj+"";
		list=baseDao.find(hql);
		return list;
	}

	public Long JoinTopicCount(PgcTopic pt) {   //查看话题参与讨论人数
		String hql="select count(id) from UgcTopicJoin where toTopicId=("+pt.getId()+")";
		return baseDao.CountHql(hql);
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> oneArtcontent(PgcArticle pa,Integer page,String time) {//--3.查看显示用户参与评论的
		List<Object[]> list=new ArrayList<Object[]>();
		String hql="select a.content,a.user_id,b.nickname,b.photo from ugc_comment as a,ugc_user as b where a.user_id=b.id and to_content_id=("+pa.getId()+") and to_content_type=1 and create_time<? order by create_time desc";
		list=baseDao.findfenye((page-1)*10, 10, hql, time,UgcComment.class);
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Object> listvideo(Integer page,String time) {//--4查询显示视频方法
		String hql="select * from pgc_video_article where create_time<? order by create_time desc";
		List<Object> list=new ArrayList<Object>();
		list=baseDao.findpage((page-1)*3, 3, hql, time,PgcVideoArticle.class);  //---通过时间 查询最新视频3条数据
		return list; 
	}

	public Object topicuserinf(UgcUser u) {
		return baseDao.find(UgcUser.class, u.getId());
	}

	public Long TopicCommentlikeCount(Object obj) {  //----统计话题点赞个数
		String hql="select count(id) from UgcLike where toContentId=("+obj+") and toContentType=4";
		return baseDao.CountHql(hql);
	}

	public Long TopicCommentIslike(UgcUser u,Object obj) {//----判断用户 对讨论是否点赞
		String hql="select count(id) from UgcLike where toContentId=("+obj+") and toContentType=4 and userId=("+u.getId()+")";
		return baseDao.CountHql(hql);
	}

	public boolean UpdatePv(PgcArticle pa) {//---更新文章浏览量
		return baseDao.update(pa);
	}

	public boolean UpdateTopicPv(PgcTopic pt) {//--更新话题的浏览量
		return baseDao.update(pt);
	}

	public Object ObjectTopic(PgcTopic pt) {  //--话题信息
		return baseDao.find(PgcTopic.class, pt.getId());
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> ListAllContents(Integer page) {//---查找所有内容 包括文章话题
		String sql="SELECT a.id id_key,a.user_id,a.title,a.image,a.pv,1 as 'content_type',a.add_time create_time,b.nickname,b.photo FROM pgc_article as a,ugc_user as b where a.user_id=b.id " +
				"UNION SELECT c.id id_key,c.user_id,c.title,c.image_url image,c.pv,2 as 'content_type',c.create_time,b.nickname,b.photo FROM pgc_topic as c,ugc_user as b where c.user_id=b.id ORDER BY create_time DESC LIMIT "+(page-1)*10+",10";
		return baseDao.Bysql(sql, null);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> ListSearchTags() {//列出标签
		String sql="SELECT id id_key,text tags from tags_content GROUP BY RAND() LIMIT 10";
		return baseDao.Bysql(sql, null);
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> MyCollect(UgcCollect uc,String time,Integer page) {
		String sql="select content_id,content_type,create_time from ugc_collect " +
				"where user_id="+uc.getUserId()+" AND create_time<'"+time+"' LIMIT "+(page-1)*10+",10";
		return baseDao.Bysql(sql, UgcCollect.class);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> ArticleInf(Object obj) {//查看文章
		String sql="SELECT a.title,a.image,a.user_id,b.nickname,b.photo FROM pgc_article as a,ugc_user as b WHERE a.user_id=b.id and a.id="+obj+"";
		return baseDao.Bysql(sql, null);
	}
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> TopicInf(Object obj) {//查看话题
		String sql="SELECT a.title,a.image_url image,a.user_id,b.nickname,b.photo FROM pgc_topic as a,ugc_user as b WHERE a.user_id=b.id and a.id="+obj+"";
		return baseDao.Bysql(sql, null);
	}

}
